#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h> /* ioctl */
#include <time.h>

#include "public.h"

static struct led_info * info;

int xy_led_open(void)
{
  int dev;

  dev = -1;
  dev = open(LEDS, O_RDONLY);
  if (dev <= 0) {
    ac_traces(MSG_LEVEL_ERROR, "open led device fial:%d\n", dev);
    return false;
  }

  info = (struct led_info *)malloc(sizeof(struct led_info));
  if (!info) {
    ac_traces(MSG_LEVEL_ERROR, "malloc fail.");
    return false;
  }

  info->dev = dev;

  return true;
}

/*
 * green led operation
 */
static int green_bright(struct led_info * info)
{
  return ioctl(info->dev, GREEN_LED_ON);
}

static int green_extinguish(struct led_info * info)
{
  return ioctl(info->dev, GREEN_LED_OFF);
}

static int green_flash(struct led_info * info)
{

  return true;
}

void xy_green_bright(void)
{
  if(!green_bright(info))
    ac_traces(MSG_LEVEL_INFO, "green light bright");
  else
    ac_traces(MSG_LEVEL_INFO, "green bright ioctl fail.");
}

void xy_green_extinguish(void)
{
  if(!green_extinguish(info))
    ac_traces(MSG_LEVEL_INFO, "green light extinguish");
  else
    ac_traces(MSG_LEVEL_INFO, "green extinguish ioctl fail.");
}

void xy_green_flash(void)
{
  green_flash(info);
}

/*
 * yellow led operation
 */
static int yellow_bright(struct led_info * info)
{
  return ioctl(info->dev, YELLOW_LED_ON);
}

static int yellow_extinguish(struct led_info * info)
{
  return ioctl(info->dev, YELLOW_LED_OFF);
}

static int yellow_flash(struct led_info * info)
{
  return true;
}

void xy_yellow_bright(void)
{
  if(!yellow_bright(info))
    ac_traces(MSG_LEVEL_INFO, "yellow light bright");
  else
    ac_traces(MSG_LEVEL_INFO, "yellow bright ioctl fail.");
}

void xy_yellow_extinguish(void)
{
  if(!yellow_extinguish(info))
    ac_traces(MSG_LEVEL_INFO, "yellow light extinguish");
  else
    ac_traces(MSG_LEVEL_INFO, "yellow extinguish ioctl fail.");
}



/*
 * red led operation
 */
static int red_bright(struct led_info * info)
{
  return ioctl(info->dev, RED_LED_ON);
}

static int red_extinguish(struct led_info * info)
{
  return ioctl(info->dev, RED_LED_OFF);
}

static int red_flash(struct led_info * info)
{
  return true;
}

void xy_red_bright(void)
{
  if(!red_bright(info))
    ac_traces(MSG_LEVEL_INFO, "red light bright");
  else
    ac_traces(MSG_LEVEL_INFO, "red bright ioctl fail.");
}

void xy_red_extinguish(void)
{
  if(!red_extinguish(info))
    ac_traces(MSG_LEVEL_INFO, "red light extinguish");
  else
    ac_traces(MSG_LEVEL_INFO, "red extinguish ioctl fail.");
}
